#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from sheepomatic_io import Csv_file
from sheepomatic import Sheep,Shepherd

class Idle_shepherd_getter:
   @classmethod
   def get_used_shepherd_set(cls,couples):
      used_shepherds =set([]) 
      for sheep_key,shepherd_key,happy,weight in couples:
         used_shepherds.add(shepherd_key)
      return used_shepherds

   @classmethod
   def read_couples(cls,filename):
      f = open(filename, 'r')
      couple_list = []
      #
      for line in f.readlines():
         line = line.strip()
         couple_list.append(tuple(line.split(",")))
      f.close()
      return couple_list

def main(args):
    # review parameters
    shepherd_file = args[0]
    shepherd_conf = args[1]
    match_file = args[2]
    # read shepherds
    shepherds_list = Csv_file.read_and_interpret_shepherd_file(shepherd_file,shepherd_conf)
    # read file with couples
    couples = Idle_shepherd_getter.read_couples(match_file)
    used_shepherds = Idle_shepherd_getter.get_used_shepherd_set(couples)
    # re-open and rewrite sheep file
    f = open(shepherd_file, 'r')
    firstline = f.readline()
    firstline = firstline.strip()
    print firstline
    i=0
    for line in f.readlines():
       line = line.strip()
       if shepherds_list[i].mail not in used_shepherds:
          print line
       i+=1

if __name__ == '__main__':
    args = sys.argv[1:]
    main(args)
